home *** CD-ROM | disk | FTP | other *** search
- %{
-
- # include <ctype.h>
-
- int line;
- %}
-
-
- /*******************************************************\
- * *
- * X_reference program for YACC files *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
- * *
- * Cathy Taylor, *
- * c/o Department of Computing, *
- * University of Lancaster, *
- * Bailrigg, Lancaster, England. *
- * Date : Fri Jul 4 00:50:04 BST 1986 *
- * *
- \*******************************************************/
-
-
- /***********************************************\
- * *
- * Yacc Input Syntax *
- * ~~~~~~~~~~~~~~~~~ *
- * *
- * Adapted from the document *
- * 'YACC - Yet Another Compiler Compiler' *
- * by *
- * S. C. Johnson *
- * *
- * Date: Tue Jul 1 02:40:18 BST 1986 *
- * *
- \***********************************************/
-
-
- %token IDENTIFIER CHARACTER NUMBER
- %token LEFT RIGHT NONASSOC TOKEN PREC TYPE START UNION
- %token PER PERCURL ACT
- %token COLON SEMICOLON COMMA OR LESS GREATER
-
- %start spec
-
- %%
-
- spec
- : defs PER rules tail
- {
- printf("\n\n");
- yyclearin;
- return(0);
- }
- ;
-
- tail
- : /* empty */
- | PER
- | error
- ;
-
- defs
- : /* empty */
- | def_bk
- ;
-
- def_bk
- : def
- | def_bk def
- ;
-
- def
- : START IDENTIFIER
- | UNION
- | PERCURL
- | rword tag nlist
- | tokendef
- | error
- ;
-
- rword
- : LEFT
- | RIGHT
- | NONASSOC
- | TYPE
- ;
-
- tokendef
- : TOKEN tokenlist
- ;
-
- tokenlist
- : IDENTIFIER
- {
- yyaction(ON_C_IDENT,line);
- }
- | tokenlist opt_comma IDENTIFIER
- {
- yyaction(ON_C_IDENT,line);
- }
- ;
- tag
- : /* empty */
- | LESS IDENTIFIER GREATER
- ;
-
- nlist
- : nmno
- | nlist opt_comma nmno
- ;
-
- opt_comma
- : /* empty */
- | COMMA
- ;
-
- nmno
- : IDENTIFIER opt_num
- ;
-
- opt_num
- : /* empty */
- | NUMBER
- ;
- rules
- : rule
- | rules rule
- ;
-
- rule
- : IDENTIFIER
- {
- yyaction(ON_C_IDENT,line);
- }
- COLON body SEMICOLON
- | error SEMICOLON
- ;
-
- body
- : body_block
- | body OR body_block
- ;
-
- body_block
- : /* empty */
- | body_block body_entity
- ;
-
- body_entity
- : opt_prec id_ent
- | ACT
- ;
-
- id_ent
- : IDENTIFIER
- {
- yyaction(ON_IDENT,line);
- }
- | CHARACTER
- ;
-
- opt_prec
- : /* empty */
- | PREC
- ;
-
-
- %%
-
- # include "lex.yy.c"
-
- #define ON_C_IDENT 000
- #define ON_IDENT 001
-
- #define MAXIDENTS 1000
- #define MAXCHARS 100
- #define MAXDEFS 20
- #define MAXOCCS 1000
-
- struct IREC {
- char ident[MAXCHARS];
- int desc[MAXDEFS];
- int nextdesc;
- int occ[MAXOCCS];
- int nextocc;
- } table[MAXIDENTS];
-
-
- yyaction (action,ln)
- int action;
- int ln;
- {
- int id;
-
- id = 0;
- while ( strcmp(table[id].ident,yytext) != 0 && strcmp(table[id].ident,"") != 0 )
- id++;
-
- if ( strcmp(table[id].ident, yytext) != 0 )
- {
-
- /*******************************************************\
- * *
- * New non-terminal to be stored. *
- * No distinction is made here between tokens *
- * and (non) terminals. *
- * *
- \*******************************************************/
-
- strcpy(table[id].ident,yytext);
- table[id].nextdesc = 0;
- table[id].nextocc = 0;
- } /* fi */
-
- switch (action) {
- case ON_C_IDENT:
-
- /*******************************************************\
- * *
- * Add to list of definition lines. *
- * *
- \*******************************************************/
-
- table[id].desc[table[id].nextdesc++] = ln;
- break;
-
- case ON_IDENT:
-
- /*******************************************************\
- * *
- * Add to list of occurance lines. *
- * *
- \*******************************************************/
-
- table[id].occ[table[id].nextocc++] = ln;
- break;
-
- default :
- fprintf (stdout, "yyaction: invalid action\n");
- return (-1);
- } /* hctiws */
- return (0);
- } /* corp */
-
- nline(ln)
- int ln;
- {
- printf("%4d :\t",ln);
- }
-
-
- char declared_at_mark[] = "*";
- char occurs_at_mark[] = "";
- char token_maybe[] = "is not declared";
- char start_maybe[] = "never occurs on rhs of rule - start rule?";
-
- /*
- * Strings for output
- */
-
- main ()
- {
- int ind,id;
-
- strcpy(table[0].ident,"");
-
- line = 0;
- nline(++line);
-
- yyparse ();
-
- printf("\n\n~~~~~ Start of X-ref ~~~~~\n");
- id = 0;
- while( strcmp(table[id].ident,"") != 0 )
- {
- printf("\n%-20s ",table[id].ident);
- if (table[id].nextdesc == 0 )
- printf(" : %s",token_maybe);
- else
- {
- ind = 0;
- printf("%4d:",table[id].desc[ind++]);
- for ( ind=1; ind < table[id].nextdesc ; ind++)
- printf(" %s%4d",declared_at_mark,table[id].desc[ind]);
- }
- if (table[id].occ[0] == 0)
- printf(" %s",start_maybe);
- else
- {
- for ( ind = 0; ind < table[id].nextocc ; ind++ )
- printf(" %s%4d",occurs_at_mark,table[id].occ[ind]);
- }
- id++;
- }
- printf("\n\n~~~~~ End of X-ref ~~~~~\n");
- } /* niam */
-
- yyerror(mess)
- char *mess;
- {
- printf("\n\t%s\n",mess);
- } /* corp */
-